﻿layui.config({
    version: true,
    base: '/Plugin/'
}).extend({ //设定模块别名
    md5: 'md5', //如果 mymod.js 是在根目录，也可以不用设定别名
    common: 'common' //相对于上述 base 目录的子目录
    }).use(['element', 'jquery', 'layer', 'table', 'form', 'md5', 'common'], function () {
    var element = layui.element,
        layer = layui.layer,
        table = layui.table,
        form = layui.form,
        md5 = layui.md5,
        common = layui.common,
        $ = layui.jquery;
    //必须在框架内使用
    common.noneIframe();
    var tableUser = null;
    var tableUserRoleIn = null;
    var tableUserRoleOut = null;
    var tableload = {
        tableuser: function () {
            tableUser = table.render({
                elem: '#tableUser',
                url: '/Ashx/admin.ashx', //数据接口
                method: 'post',
                toolbar: "#toolbarBtn",
                where: { functionname: 'jhy_user_query' },
                cols: [[ //表头           
                    { field: 'user_account', title: '登录账号', event: "user_account", style: "cursor:pointer" },
                    { field: 'user_name', title: '姓名', event: "user_name", style: "cursor:pointer" },
                    { field: 'user_role', title: '关联角色', event: "user_role", style: "cursor:pointer" },
                    { title: '操作', toolbar: "#toolbarDone", width: 85 }
                ]]
            });
        }, tableuserrolein: function (user_id) {
            tableUserRoleIn = table.render({
                elem: '#tableUserRoleIn',
                url: '/Ashx/admin.ashx', //数据接口
                method: 'post',
                where: { functionname: 'jhy_user_role_query', user_id: user_id,user_field:"true" },
                cols: [[ //表头           
                    { field: 'role_name', title: '角色名称'},                   
                    { title: '操作', templet: "<div><div class='layui-inline' lay-event='out'><i class='layui-icon layui-icon-circle'></i></div></div>", width: 60, align: "center", style: "cursor:pointer" }
                ]]
            });
        }, tableuserroleout: function (user_id) {
            tableUserRoleOut = table.render({
                elem: '#tableUserRoleOut',
                url: '/Ashx/admin.ashx', //数据接口
                method: 'post',              
                where: { functionname: 'jhy_user_role_query', user_id: user_id, user_field: "" },
                cols: [[ //表头           
                    { field: 'role_name'},                 
                    { title: '操作', templet: "<div><div class='layui-inline' lay-event='in'><i class='layui-icon layui-icon-circle'></i></div></div>", width: 60, align: "center", style: "cursor:pointer" }
                ]]
            });
        }
    }

    tableload.tableuser();

    //添加用户
    table.on('toolbar(tableUser)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'add':
                //——————————————————————————
                layer.open({
                    title: "添加用户",
                    type: 1,
                    btn: ["保存"],
                    skin: "jhy-layer-css",
                    content: $("#layerAdd"),
                    success: function (layero, index) {
                        $("#account").val("");
                        $("#name").val("");
                        $("#password").val("");
                    },
                    yes: function (index, layero) {
                        var user_account = $.trim($("#account").val());
                        var user_name = $.trim($("#name").val());
                        var user_password = md5($.trim($("#password").val()));
                        var error = "";
                        if (user_account == "") {
                            error += "账号不能为空<br>";
                        }
                        if (user_name == "") {
                            error += "姓名不能为空<br>";
                        }
                        if ($.trim($("#password").val()) == "") {
                            error += "密码不能为空<br>";
                        }
                        if (error.length > 0) {
                            layer.msg(error);
                            return false;
                        }
                        $.ajax({
                            type: "post",
                            url: "/Ashx/admin.ashx",
                            data: { functionname: "jhy_user_add", user_account: user_account, user_name: user_name, user_password: user_password },
                            success: function (msg) {
                                if (msg == 'true') {
                                    layer.msg("成功");
                                    tableUser.reload();
                                } else {
                                    layer.msg("失败");
                                    tableUser.reload();
                                }
                                layer.close(index);
                            }
                        });
                    }

                });

                //——————————————————————————————
                break;
        };
    });

    //修改账号，修改姓名，修改角色，修改密码，删除
    table.on('tool(tableUser)', function (obj) {
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        var user_id = data.user_id;
        if (layEvent === 'user_account') { //修改账号
            layer.prompt({
                formType: 0,
                value: data.user_account,
                title: '修改账号'
            }, function (value, index, elem) {
                if (data.user_account == value) {
                    layer.close(index);
                } else {
                    $.ajax({
                        type: "post",
                        url: "/Ashx/admin.ashx",
                        data: { functionname: "jhy_user_edit", user_id: user_id, user_field: 'user_account', user_para: value },
                        success: function (msg) {
                            if (msg == 'true') {
                                layer.msg("成功");
                                obj.update({
                                    user_account: value
                                });
                            } else {
                                layer.msg("失败");
                                tableUser.reload();
                            }
                            layer.close(index);
                        }
                    });
                }

            });



        } else if (layEvent === 'user_name') { //修改姓名
            layer.prompt({
                formType: 0,
                value: data.user_name,
                title: '修改姓名'
            }, function (value, index, elem) {
                if (data.user_name == value) {
                    layer.close(index);
                } else {
                    $.ajax({
                        type: "post",
                        url: "/Ashx/admin.ashx",
                        data: { functionname: "jhy_user_edit", user_id: user_id, user_field: 'user_name', user_para: value },
                        success: function (msg) {
                            if (msg == 'true') {
                                layer.msg("成功");
                                obj.update({
                                    user_name: value
                                });
                            } else {
                                layer.msg("失败");
                                tableUser.reload();
                            }
                            layer.close(index);
                        }
                    });
                }

            });
        } else if (layEvent === 'delete') { //删除
            layer.confirm('真的删除【' + data.user_name + '】?', {icon:3,title:"删除用户"}, function (index) {
                $.ajax({
                    type: "post",
                    url: "/Ashx/admin.ashx",
                    data: { functionname: "jhy_user_delete", user_id: user_id},
                    success: function (msg) {
                        if (msg == 'true') {
                            layer.msg("成功");
                            obj.del();
                        } else {
                            layer.msg("失败");
                            tableUser.reload();
                        }                      
                    }
                });              
                layer.close(index);              
            });
        } else if (layEvent === 'password') { //修改密码
            layer.prompt({
                formType: 0,
                title: '修改密码'
            }, function (value, index, elem) {
                $.ajax({
                    type: "post",
                    url: "/Ashx/admin.ashx",
                    data: { functionname: "jhy_user_edit", user_id: user_id, user_field: 'user_password', user_para: md5(value) },
                    success: function (msg) {
                        if (msg == 'true') {
                            layer.msg("成功");
                            obj.update({
                                user_password: md5(value)
                            });
                        } else {
                            layer.msg("失败");
                            tableUser.reload();
                        }
                        layer.close(index);
                    }
                });


            });
        } else if (layEvent === 'user_role') { //修改角色
            layer.open({
                title: "管理用户角色",
                type: 1,
                btn: ["关闭窗口"],
                skin: "jhy-layer-css",
                area:["100%","100%"],
                content: $("#layerUserRole"),
                success: function (layero, index) {
                    tableload.tableuserrolein(user_id);
                    tableload.tableuserroleout(user_id);
                },
                yes: function (index, layero) {
                    layer.close(index);
                }

            });
        }
    });

    //添加系统角色
    table.on('tool(tableUserRoleIn)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象

        if (layEvent === 'out') { //查看
            var user_id = data.user_id;
            var role_id = data.role_id;
            var field = 'delete';
            $.ajax({
                type: "post",
                url: "/Ashx/admin.ashx",
                data: { functionname: "jhy_user_role_edit", user_id: user_id, role_id: role_id, field: field },
                success: function (msg) {
                    if (msg == 'true') {
                        layer.msg("成功");
                        obj.del();
                        tableUser.reload();
                        tableUserRoleOut.reload();
                    } else {
                        layer.msg("失败");
                        tableUser.reload();
                    }                   
                }
            });
        } 
    });


    //删除系统角色
    table.on('tool(tableUserRoleOut)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象

        if (layEvent === 'in') { //查看
            var user_id = data.user_id;
            var role_id = data.role_id;
            var field = 'add';
            $.ajax({
                type: "post",
                url: "/Ashx/admin.ashx",
                data: { functionname: "jhy_user_role_edit", user_id: user_id, role_id: role_id, field: field },
                success: function (msg) {
                    if (msg == 'true') {
                        layer.msg("成功");
                        obj.del();
                        tableUser.reload();
                        tableUserRoleIn.reload();
                    } else {
                        layer.msg("失败");
                        tableUser.reload();
                    }
                }
            });
        } 
    });


});  
